 //src/main/java/com/neuedu/hisadmin/service/impl/SchedulingServiceImpl.java
package com.neuedu.hisadmin.service.impl;

import com.neuedu.hisadmin.dto.DoctorScheduleDTO;
import com.neuedu.hisadmin.dto.Result;
import com.neuedu.hisadmin.entity.Scheduling;
import com.neuedu.hisadmin.mapper.SchedulingMapper;
import com.neuedu.hisadmin.service.ISchedulingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.servlet.View;

import java.time.LocalDate;
import java.util.List;


 @Service
 public class SchedulingServiceImpl extends ServiceImpl<SchedulingMapper, Scheduling> implements ISchedulingService {

    @Autowired
    private SchedulingMapper schedulingMapper;
    private Scheduling scheduling;
     @Autowired
     private View error;

     @Override
    public List<DoctorScheduleDTO> getDoctorSchedules(String deptName, String doctorName, String registLevel) {
        // LocalDate currentDate = LocalDate.now(); // 获取当前日期
         return schedulingMapper.findDoctorSchedules(
                deptName,
                doctorName,
                registLevel

        );
   }
     @Override
     public List<DoctorScheduleDTO> getSchedulingWithRelationsByCondition(String deptName, String doctorName, String registLevel) {
         // 处理空值（前端可能传空，此时查全部）
         deptName = deptName == null ? "%" : "%" + deptName + "%";
         doctorName = doctorName == null ? "%" : "%" + doctorName + "%";
         registLevel = registLevel == null ? "%" : "%" + registLevel + "%";

         return baseMapper.findDoctorSchedules(deptName, doctorName, registLevel);
     }
     // 新增方法实现
     @Transactional // 事务控制
     @Override
     public boolean newScheduling(Scheduling scheduling) {
         // 调用 MyBatis-Plus 自带的 save 方法
         return save(scheduling);
      }

     @Override
     public boolean deleteScheduling(Integer id)
     {
         Scheduling scheduling=new Scheduling();
         scheduling.setID(id);
         scheduling.setDelMark(0);
         return updateById(scheduling);
   }


    @Override
    public Result<Boolean> updateScheduling(Scheduling scheduling) {
    try {
        // updateById 返回 int，接收影响行数
        int affectedRows = schedulingMapper.updateById(scheduling);
        // 影响行数 > 0 表示更新成功
        if (affectedRows > 0) {
            return Result.success(true);
        } else {
            // 影响行数 = 0，更新失败（比如主键不存在、更新内容和数据库一致）
            return Result.error(400, "更新失败，无数据变动");
        }
    } catch (Exception e) {
        log.error("更新排班异常: ", e);
        // 捕获到异常，返回错误响应
        return Result.error(500, "服务器内部错误，更新失败");
    }
    // 这里不用额外处理返回，因为 try - catch 已经覆盖所有分支
}
 }

